<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" class="demo-form-inline" @submit.native.prevent>
      <el-form-item label="子订单">
        <el-input v-model="listQuery.q_orderSn" size="small"></el-input>
      </el-form-item>
      <el-form-item label="父订单">
        <el-input v-model="listQuery.q_orderPsn" size="small"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="listQuery.q_memberName" size="small"></el-input>
      </el-form-item>
      <el-form-item label="订单类型">
        <el-select
          class="toolbar-lable-right"
          v-model="listQuery.q_orderType"
          placeholder="请选择" size="small"
        >
          <el-option
            v-for="item in orderType"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收货电话">
        <el-input v-model="listQuery.q_mobile" size="small"></el-input>
      </el-form-item>
      <el-form-item label="商家">
        <el-select
          class="toolbar-lable-right"
          v-model="listQuery.q_sellerId"
          placeholder="请选择"
          size="small"
        >
          <el-option
            v-for="item in sellers"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="details" v-permission="['admin/order/orders/details']">订单详情</el-button>
      <el-button size="small" type="primary" @click="submitpayd" v-permission="['admin/order/orders/submitpay']">确认收款</el-button>
    </div>
    <el-table
      ref="singleTable"
      v-loading="listLoading"
      :data="dataList"
      border
      fit
      highlight-current-row
      @current-change="handleCurrentChange"
      style="width: 100%">
      <el-table-column
        type="index"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        label="购买商品"
        width="300"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <div>
            <img width="80" height="80" v-for="item in scope.row.orderProductList" :src="$imagePath+item.productLeadPicpath" style="vertical-align: middle;">
            <span v-if="scope.row.orderProductList.length === 1">{{ scope.row.orderProductList[0].productName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        property="orderSn"
        width="200"
        label="子订单号">
      </el-table-column>
      <el-table-column
        property="orderPsn"
        width="200"
        label="父订单号">
      </el-table-column>
      <el-table-column
        property="memberName"
        width="150"
        label="买家用户名">
      </el-table-column>
      <el-table-column
        property="orderType"
        width="120"
        label="订单类型">
        <template slot-scope="scope">
          {{ codeBox.ORDER_TYPE[scope.row.orderType] }}
        </template>
      </el-table-column>
      <el-table-column
        property="paymentStatus"
        width="100"
        label="付款状态">
        <template slot-scope="scope">
          {{ codeBox.ORDER_PAYMENT_STATUS[scope.row.paymentStatus] }}
        </template>
      </el-table-column>
      <el-table-column
        property="orderState"
        width="100"
        label="订单状态">
        <template slot-scope="scope">
          {{ codeBox.ORDERS_ORDER_STATE[scope.row.orderState] }}
        </template>
      </el-table-column>
      <el-table-column
        property="source"
        width="100"
        label="订单来源">
        <template slot-scope="scope">
          {{ codeBox.MEMBER_SOURCE[scope.row.source] }}
        </template>
      </el-table-column>
      <el-table-column
        property="sellerName"
        width="150"
        label="店铺">
      </el-table-column>
      <el-table-column
        property="moneyProduct"
        width="100"
        label="商品金额">
      </el-table-column>
      <el-table-column
        property="moneyOrder"
        width="100"
        label="订单总金额">
      </el-table-column>
      <el-table-column
        property="moneyLogistics"
        width="100"
        label="物流金额">
      </el-table-column>
      <el-table-column
        property="moneyCoupon"
        width="100"
        label="优惠券金额">
      </el-table-column>
      <el-table-column
        property="moneyActFull"
        width="120"
        label="订单满减金额">
      </el-table-column>
      <el-table-column
        property="moneyDiscount"
        width="200"
        label="优惠总额（含单品立减）">
      </el-table-column>
      <el-table-column
        property="moneyBack"
        width="120"
        label="退款金额">
      </el-table-column>
      <el-table-column
        property="moneyIntegral"
        width="120"
        label="积分换算金额">
      </el-table-column>
      <el-table-column
        property="integral"
        width="120"
        label="使用积分">
      </el-table-column>
      <el-table-column
        property="name"
        width="120"
        label="收货人">
      </el-table-column>
      <el-table-column
        property="mobile"
        width="120"
        label="收货电话">
      </el-table-column>
      <el-table-column
        property="addressAll"
        width="200"
        label="省市区"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        property="addressInfo"
        width="200"
        label="详细地址"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        property="invoiceStatus"
        width="150"
        label="发票状态">
        <template slot-scope="scope">
          {{ codeBox.ORDER_INVOICE_STATUS[scope.row.invoiceStatus] }}
        </template>
      </el-table-column>
      <el-table-column
        property="invoiceTitle"
        width="150"
        label="发票抬头">
      </el-table-column>
      <el-table-column
        property="invoiceType"
        width="150"
        label="发票类型">
      </el-table-column>
      <el-table-column
        property="taxNum"
        width="200"
        label="纳税人识别号">
      </el-table-column>
      <el-table-column
        property="paymentName"
        width="150"
        label="支付方式">
      </el-table-column>
      <el-table-column
        property="logisticsName"
        width="150"
        label="物流名称">
      </el-table-column>
      <el-table-column
        property="logisticsNumber"
        width="150"
        label="快递单号">
      </el-table-column>
      <el-table-column
        property="deliverTime"
        width="200"
        label="发货时间">
      </el-table-column>
      <el-table-column
        property="updateTime"
        width="200"
        label="修改时间">
      </el-table-column>
      <el-table-column
        property="evaluateState"
        width="100"
        label="是否评价">
        <template slot-scope="scope">
          {{ codeBox.ORDERS_EVALUATE[scope.row.evaluateState] }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="70" align="center">
        <template slot-scope="scope">
          <el-button @click="detailsRow(scope.row.id)" type="text" size="small">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { listOrders, getSellers, submitpayOrders } from '@/api/order'
import Pagination from '@/components/Pagination'
import { Message } from 'element-ui'
import { getCodeManager,codeToArray} from '@/api/all'
export default {
  name: 'orderslist',
  components: { Pagination },
  data() {
    return {
      dataList: [],
      orderState: [],
      orderType: [],
      sellers: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        q_orderSn: '',
        q_orderPsn: '',
        q_memberName: '',
        q_orderState: '4',
        q_orderType: '',
        q_mobile: '',
        q_sellerId: ''
      }
    }
  },
  created() {
    getCodeManager('ORDERS_ORDER_STATE, ORDER_PAYMENT_STATUS, ORDER_INVOICE_STATUS, ORDER_TYPE, ORDERS_EVALUATE, MEMBER_SOURCE', () => {
      this.orderState = codeToArray('ORDERS_ORDER_STATE');
      this.orderType = codeToArray('ORDER_TYPE');
    })
    this.getList()
    getSellers().then(response => {
      this.sellers=[
        ...response.data.rows.map((item)=>{return  {
          name:item.sellerName,
          value:item.id
        }})
      ]
    })
  },
  methods:{
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      listOrders(this.listQuery).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    reset() {
      this.listQuery = {
        page: 1,
        limit: 20,
        q_orderSn: '',
        q_orderPsn: '',
        q_memberName: '',
        q_orderState: '4',
        q_orderType: '',
        q_mobile: '',
        q_sellerId: ''
      }
      this.getList()
    },
    details() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      this.$router.push({
        path: "/orders/ordersdetails",
        query: { id: this.currentRow.id }
      });
    },
    detailsRow(id){
      this.$router.push({
        path: "/orders/ordersdetails",
        query: { id: id }
      });
    },
    submitpayd() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.paymentCode != 'OFFLINE') {
        Message.error('只有货到付款的订单可确认收款')
        return
      }
      if(this.currentRow.orderState != 4 && this.currentRow.orderState != 5) {
        Message.error('已发货或者已完成的订单才能确认收款')
        return
      }
      if(this.currentRow.paymentStatus == 1) {
        Message.error('该订单已经付款，请勿重复操作')
        return
      }
      this.$confirm('此订单确认收款吗？', '提示', {
        onfirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        submitpayOrders({id: this.currentRow.id}).then(res => {
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
      })
    }

  }
}
</script>

<style>
</style>
